package online.sh.cn.fy.users.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import online.sh.cn.fy.common.utils.CookieUtil;
import online.sh.cn.fy.common.utils.DES3Util;
import online.sh.cn.fy.common.utils.ResponseUtils;
import online.sh.cn.fy.common.utils.TokenUtils;
import online.sh.cn.fy.common.utils.WxUtils;

/**
 * 保存，认证用户信息
 */
@WebServlet("/saveuser")
public class FyUserSaveServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		WxUtils wx=new WxUtils();
		String mobile=request.getParameter("mobile");
		String lockcode=request.getParameter("lockcode");
		String school=request.getParameter("school");
		String building=request.getParameter("building");
		String useryear=request.getParameter("useryear");
		String username=request.getParameter("username");
		String usermajor=request.getParameter("usermajor");
		String coder=request.getParameter("coder");
		String belong=wx.getMoblieinfo(mobile).getString("province");
		String operator=wx.getMoblieinfo(mobile).getString("catName");
		String openid=CookieUtil.getCookieValueByName(request, "openid");
		String exmobile=CookieUtil.getCookieValueByName(request, "messageMobile");
		String excode=CookieUtil.getCookieValueByName(request, "messageCode");
		String exlockcode=request.getSession().getAttribute("lockcode").toString().toLowerCase();   //图形验证码
//		openid=CookieUtil.decodeCookie(openid);
//		exmobile=CookieUtil.decodeCookie(exmobile);
//		excode=CookieUtil.decodeCookie(excode);
		//判断验证码和发送手机号
		System.out.println(exmobile+"##########"+excode);
		System.out.println(mobile+"*********"+coder);
		System.out.println("新认证用户openid:"+openid);
		JSONObject json=new JSONObject();
		if(null!=openid && ""!=openid){
			if(null!=lockcode && ""!=lockcode && exlockcode.equals(lockcode.toLowerCase())){
				if (null!=exmobile && null!=excode) {
					try {
						openid=DES3Util.decrypt(openid);
						exmobile=DES3Util.decrypt(exmobile);
						excode=DES3Util.decrypt(excode);
					} catch (Exception e) {
						log(e.getMessage());
					}
					if (exmobile.equals(mobile) && excode.equals(coder)) {
						TokenUtils to=new TokenUtils();
						String openidis=to.getUserMobile(openid);
						if(null!=openidis && ""!=openidis){    //验证是否已经注册
							json.put("phone", openidis);
							json.put("result", "8");  
							ResponseUtils.renderJson(response, json.toString());
						}else{
							String checkmobile=to.getMobileBand(mobile);
							if (checkmobile.equals(mobile)) {
								to.deleteMobileBand(mobile);
								to.insertUser(openid, mobile, belong, operator, username, school, building, useryear, usermajor);
								to.savePointsEvent(openid, "", "新注册'Young'粉", "+80");
							}else{
								to.insertUser(openid, mobile, belong, operator, username, school, building, useryear, usermajor);
								to.savePointsEvent(openid, "", "新注册'Young'粉", "+80");
							}
							json.put("result", "0"); //验证成功 入库 
							ResponseUtils.renderJson(response, json.toString());
						}
						
					}else{
						json.put("result", "1"); //预留手机号或者验证码错误
						ResponseUtils.renderJson(response, json.toString());
					}
				}else{
					json.put("result", "2");   //未进行短消息发送的验证
					ResponseUtils.renderJson(response, json.toString());
				}
			}else{
				json.put("result", "4");   //图形验证码错误
				ResponseUtils.renderJson(response, json.toString());
			}
			
		}else{
			json.put("result", "3");   //openid未获取
			ResponseUtils.renderJson(response, json.toString());
		}
	}
}
